/**
 * Created by Ljh on 2016/11/16.
 */
$(function () {
    FastClick.attach(document.body);
    if (typeof Stars == "undefined") {
        var Stars = {};
    }
    Stars.result = {
        variable: {
            tmpData: []//存储临时数据
        },
        init: function () {
            var that = this;
            var url = 'http://newos.glassmarket.cn/zhsm/test_2017yunshi.php';
            var arrDate = that.requestPara('date').split('|');
            var dataObj = {
                "xing": "",
                "name": that.requestPara('name'),
                "sex": that.requestPara('gender'),
                "y": arrDate[0],
                "m": arrDate[1],
                "d": arrDate[2],
                "h": arrDate[3],
                "i": arrDate[4],
                "isfanti": that.requestPara('isfanti'),
                "fortune_type": 11,
                "user_id": that.requestPara('user_id'),
                "auth_code": that.requestPara('auth_code')
            };
            that.postJsonp(url, dataObj);
            return that.variable.tmpData;
        },
        requestPara: function (paras) {
            //获取url参数
            var url = location.href;
            url = decodeURI(url);
            var paraString = url.substring(url.indexOf("?") + 1, url.length).split("&");
            var paraObj = {};
            for (var i = 0; j = paraString[i]; i++) {
                paraObj[j.substring(0, j.indexOf("=")).toLowerCase()] = j.substring(j.indexOf("=") + 1, j.length);
            }
            var returnValue = paraObj[paras.toLowerCase()];
            if (typeof(returnValue) == "undefined") {
                return "";
            } else {
                return returnValue;
            }
        },
        postJsonp: function (url, dataObj) {
            var that = this;
            $.ajax({
                url: url,
                type: 'post',
                dataType: 'xml',
                data: dataObj,
                success: function (data) {
                    data = that.xmlToJson(data);
                    console.log(data);
                    if( data.return_data.statustxt['#text'] =="成功" ){
                        //渲染模板
                        that._template( data.return_data, '#results', '#result_list');
                        //触屏滑动效果代替overflow:scroll;
                        // that._scroll({
                        //     swipeDom: document.querySelector('body'),
                        //     swipeDirection: 'y',
                        //     bufferDistance: 150,
                        //     index: 1
                        // });
                        //当未购买时，把结果存到变量中待调用
                        if(data.return_data.info.is_buy.content['#text'] == 0){
                            var tmpArr = data.return_data['fortune-list'].fortune;
                            for ( var i = 0; i < tmpArr.length; i++ ){
                                if( tmpArr[i].f_content_s['#text'] ){
                                    that.variable.tmpData.push( tmpArr[i].f_content_s['#text'] );
                                }
                            }
                        }
                        //buyOver(1);
                    }
                }
            });
        },
        xmlToJson: function (xml) {
            var that = this;
            var obj = {};
            if (xml.nodeType == 1) {
                if (xml.attributes.length > 0) {
                    obj["@attributes"] = {};
                    for (var j = 0; j < xml.attributes.length; j++) {
                        var attribute = xml.attributes.item(j);
                        obj["@attributes"][attribute.nodeName] = attribute.nodeValue;
                    }
                }
            } else if (xml.nodeType == 3) {
                obj = xml.nodeValue;
            }
            if (xml.hasChildNodes()) {
                for (var i = 0; i < xml.childNodes.length; i++) {
                    var item = xml.childNodes.item(i);
                    var nodeName = item.nodeName;
                    if (typeof(obj[nodeName]) == "undefined") {
                        obj[nodeName] = that.xmlToJson(item);
                    } else {
                        if (typeof(obj[nodeName].length) == "undefined") {
                            var old = obj[nodeName];
                            obj[nodeName] = [];
                            obj[nodeName].push(old);
                        }
                        obj[nodeName].push(that.xmlToJson(item));
                    }
                }
            }
            return obj;
        },
        bindEvent: function () {

        },
        _template: function (data, boxGet, boxRender) {
            var renderTemplate = doT.template( $(boxGet).html() );
            $( boxRender ).html( renderTemplate(data) );
        }


    }
    var tmpData = Stars.result.init();
    function buyOver(buyresult){
        alert("---->"+buyresult);
        if( buyresult==1 ){
            $('.mistiness').each(function (i, v) {
                $(v).html( tmpData[i]).addClass('test_result');
            });
        }
    }
    //$('.body').click(function () {
    //    buyOver(1);
    //})

});